﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Location.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The location.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Services.Dto
{
    using System;
    using System.Runtime.Serialization;

    /// <summary>
    /// The location.
    /// </summary>
    [DataContract]
    public class Location
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the address.
        /// </summary>
        [DataMember]
        public Address Address { get; set; }

        /// <summary>
        /// Gets or sets the id.
        /// </summary>
        [DataMember]
        public Guid ID { get; set; }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        [DataMember]
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the region.
        /// </summary>
        [DataMember]
        public Region Region { get; set; }

        /// <summary>
        /// Gets or sets a value indicating whether within 150 km.
        /// </summary>
        [DataMember]
        public bool Within150Km { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The convert.
        /// </summary>
        /// <param name="loc">
        /// The loc.
        /// </param>
        /// <returns>
        /// The <see cref="Location"/>.
        /// </returns>
        public static Location Convert(Models.Domain.Location loc)
        {
            if (loc != null)
            {
                var dto = new Location
                              {
                                  ID = loc.ID, 
                                  Address = Address.Convert(loc.Address), 
                                  Name = loc.Name, 
                                  Within150Km = loc.Within150Km, 
                                  Region = Region.Convert(loc.Region)
                              };
                return dto;
            }
            
            return null;
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Queries
{
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Locations;
    using ServiceLibrary.Services.Results;

    /// <summary>
    /// The locations query.
    /// </summary>
    [DataContract]
    public class LocationsQuery : IQuery
    {
        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <returns>
        /// The <see cref="IResult"/>.
        /// </returns>
        public override IResult Execute(ISession hsession)
        {
            var actioner = new LocationActioner(hsession, null);
            return actioner.ExecuteSearchQuery(this);
        }

        #endregion
    }
}

namespace ServiceLibrary.Services.Results
{
    using System.Collections.Generic;
    using System.Runtime.Serialization;

    using ServiceLibrary.Services.Dto;

    /// <summary>
    /// The locations result.
    /// </summary>
    [DataContract]
    public class LocationsResult : IResult
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="LocationsResult"/> class.
        /// </summary>
        public LocationsResult()
        {
            this.List = new List<Location>();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the list.
        /// </summary>
        [DataMember]
        public List<Location> List { get; set; }

        /// <summary>
        /// Gets or sets the total row count.
        /// </summary>
        [DataMember]
        public int TotalRowCount { get; set; }

        #endregion
    }
}

namespace ServiceLibrary.Services.Commands
{
    using System.Runtime.Serialization;

    using NHibernate;

    using ServiceLibrary.Actioners.Locations;
    using ServiceLibrary.Services.Dto;

    using User = ServiceLibrary.Models.Domain.User;

    /// <summary>
    /// The create location command.
    /// </summary>
    [DataContract]
    public class CreateLocationCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the new location.
        /// </summary>
        [DataMember]
        public Location NewLocation { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new LocationActioner(hsession, activeUser);
            actioner.CreateLocation(this);
        }

        #endregion
    }

    /// <summary>
    /// The update location command.
    /// </summary>
    [DataContract]
    public class UpdateLocationCommand : ICommand
    {
        #region Public Properties

        /// <summary>
        /// Gets or sets the dto.
        /// </summary>
        [DataMember]
        public Location Dto { get; set; }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="hsession">
        /// The hsession.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public override void Execute(ISession hsession, User activeUser)
        {
            var actioner = new LocationActioner(hsession, activeUser);
            actioner.UpdateLocation(this);
        }

        #endregion
    }
}